package com.markyao.recos.kill;

import com.markyao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Service
public class RecosKillServiceImpl implements RecosKillService{
    @Autowired
    @Qualifier("connectSet")
    Map<String, Set<String>> connectSet;
    @Autowired
    @Qualifier("providersMap")
    Map<String,Map<String,String>>providersMap;
    @Autowired
    @Qualifier("refrenceRegistrySet")
    Map<String,Set<String>>refrenceRegistrySet;
    @Autowired
    @Qualifier("serverRegistryMap")
    Map<String,Map<String,String>>serverRegistryMap;
    @Autowired
    @Qualifier("serviceRegistryMap")
    Map<String,Map<String,String>>serviceRegistryMap;
    @Autowired
    @Qualifier("urlMap")
    Map<String,String>urlMap;
    @Autowired
    @Qualifier("heartBeatMap")
    Map<String,Long>heartBeatMap;
    @Autowired
    @Qualifier("errorUrlMap")
    Map<String,String>errorUrlMap;

    @Autowired
    @Qualifier("killConnectKeys")
    Map<String,Set<String>>killConnectKeys;
    @Autowired
    @Qualifier("reconnectKeys")
    Map<String,Set<String>>reconnectKeys;
    @Override
    public Result killConnect(String appName, String url) {
        String targetAppname = urlMap.get(url);
        Set<String> set = connectSet.get(appName);
        set.remove(targetAppname);
        return Result.success("kill成功!");
    }

    @Override
    public Result hasKills(String appName) {
        if (killConnectKeys.containsKey(appName)) {
            Set<String> set = killConnectKeys.get(appName);
            return Result.success(set);
        }
        return Result.success(new HashSet<String>());
    }


    @Override
    public Result killOk(String appName) {
        if (killConnectKeys.containsKey(appName)) {
            killConnectKeys.remove(appName);
        }
        return Result.success("killOK");
    }

    @Override
    public Result reconnectOk(String appName) {
        if (reconnectKeys.containsKey(appName)){
            reconnectKeys.remove(appName);
        }
        return Result.success("reconnectOk");
    }

    @Override
    public Result hasReconnects(String appName) {
        if (reconnectKeys.containsKey(appName)){
            Set<String> set = reconnectKeys.get(appName);
            return Result.success(set);
        }
        return Result.success(new HashSet<String>());
    }
}
